package com.wanlian.service.impl;

import com.wanlian.common.PageCondition;
import com.wanlian.dao.RoleGroupUserDao;
import com.wanlian.entity.RoleGroupUser;
import com.wanlian.model.ResultModel;
import com.wanlian.service.RoleGroupUserService;
import com.wanlian.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class RoleGroupUserServiceImpl implements RoleGroupUserService {

    @Autowired
    private RoleGroupUserDao roleGroupUserDao;

    @Override
    public ResultModel doAdd(RoleGroupUser roleGroupUser) {
    ResultModel resultModel=new ResultModel(true);
    RoleGroupUser db_roleGroupUser=roleGroupUserDao.queryByGid(roleGroupUser.getGroupid(),roleGroupUser.getUserid());
    if(db_roleGroupUser==null){
        roleGroupUserDao.add(roleGroupUser);
        resultModel.setMessage("添加成功");
    }else{
        resultModel.setMessage("改成员已存在该用户组");
        resultModel.setBresult(false);
    }
        return resultModel;
    }

    @Override
    public ResultModel doDel(RoleGroupUser roleGroupUser) {
        ResultModel resultModel=new ResultModel(true);
        resultModel.setMessage("删除成功");
        resultModel.setBresult(true);
        roleGroupUserDao.delete(roleGroupUser.getId());
        return resultModel;
    }

    @Override
    public ResultModel queryByParam(PageCondition pageCondition) {
        return roleGroupUserDao.queryByParam(pageCondition);
    }

    @Override
    public ResultModel doEdit(RoleGroupUser roleGroupUser) {
        ResultModel resultModel=new ResultModel(true);
        RoleGroupUser db_roleGroupUser=roleGroupUserDao.queryByUid(roleGroupUser.getUserid());
        if(db_roleGroupUser==null){
            roleGroupUser.setCreateid(roleGroupUser.getCreateid());
            roleGroupUser.setLastupdatetime(DateUtil.currentTimestamp());
            roleGroupUserDao.add(roleGroupUser);
        }else{
            db_roleGroupUser.setGroupid(roleGroupUser.getGroupid());
            db_roleGroupUser.setCreateid(roleGroupUser.getCreateid());
            db_roleGroupUser.setLastupdatetime(DateUtil.currentTimestamp());
            db_roleGroupUser.setRemark(roleGroupUser.getRemark());
            roleGroupUserDao.update(db_roleGroupUser);
        }
        resultModel.setMessage("修改成功");
        resultModel.setBresult(true);
        return resultModel;
    }

    @Override
    public RoleGroupUser queryByUid(int userId) {
        return roleGroupUserDao.queryByUid(userId);
    }
}
